package com.tnsw.facebookautoliker;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Settings;
import com.facebook.model.GraphUser;
import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.ui.UpdateView;
import com.tnsw.facebookautoliker.helper.FacebookActionListener;
import com.tnsw.facebookautoliker.helper.FacebookBGManager;
import com.tnsw.facebookautoliker.helper.FacebookCommandObject;
import com.tnsw.facebookautoliker.helper.GeneralManager;
import com.tnsw.facebookautoliker.helper.TestFBAction2;
import com.tnsw.facebookautoliker.helper.command.GetPhoto;
import com.tnsw.facebookautoliker.helper.command.RefreshFriendsList;

public class FriendListUpdateView extends UpdateView implements FacebookActionListener, Request.GraphUserListCallback {

	private String tag = "FriendListUpdateView";
	private ImageView imgAndroid, imgRefresh, imgRefresh2;
	private Animation ani;
	
	private int currentDeltaY = 0;
	
	private LinearLayout updateLayout1, updateLayout2, updateLayout3;
	
	private String requestId;
	
	public FriendListUpdateView(Context context, ListView lv, LinearLayout parentView) {
		super(context, lv, parentView);
		MyLog.d(tag, "Constructur(context, lv, parentView)");
	}

	@Override
	public View getView() {
		View v = this.inflater.inflate(R.layout.updating_layout, null);
		
		this.imgAndroid = (ImageView) v.findViewById(R.id.imgAndroid);
		this.imgRefresh = (ImageView) v.findViewById(R.id.imgRefresh);
		this.imgRefresh2 = (ImageView) v.findViewById(R.id.imgRefresh2);
		
		this.ani = AnimationUtils.loadAnimation(this.context, R.anim.animation1);
		this.imgRefresh2.setAnimation(this.ani);
		
		this.updateLayout1 = (LinearLayout) v.findViewById(R.id.updateLayout1);
		this.updateLayout2 = (LinearLayout) v.findViewById(R.id.updateLayout2);
		this.updateLayout3 = (LinearLayout) v.findViewById(R.id.updateLayout3);
			
		return v;
	}
	
	@Override
	public void onPulling(int deltaY) {
		super.onPulling(deltaY);
		this.currentDeltaY = deltaY;
		MyLog.d(tag, "onPulling(deltaY=" + deltaY + ")");
		
		Bitmap bmp = ((BitmapDrawable) this.imgAndroid.getDrawable()).getBitmap();
		
		if (deltaY < bmp.getHeight()*2) {
			this.updateLayout1.setVisibility(View.VISIBLE);
			this.updateLayout2.setVisibility(View.GONE);
			this.updateLayout3.setVisibility(View.GONE);
			
			if (this.imgRefresh2.getAnimation() != null) {
				this.imgRefresh2.getAnimation().cancel();
				
			}
			
			
		} else {
			this.updateLayout1.setVisibility(View.GONE);
			this.updateLayout2.setVisibility(View.VISIBLE);
			this.updateLayout3.setVisibility(View.GONE);
			
			if (this.imgRefresh2.getAnimation() != null) {
				this.imgRefresh2.getAnimation().cancel();
				
			}
		}
	}
	
	
	
	@Override
	public void onRelease() {
		super.onRelease();
		
		if (this.updateLayout2.getVisibility() == View.VISIBLE) {
			this.updateLayout1.setVisibility(View.GONE);
			this.updateLayout2.setVisibility(View.GONE);
			this.updateLayout3.setVisibility(View.VISIBLE);
			
			if (this.imgRefresh2.getAnimation() != null) {
				this.imgRefresh2.getAnimation().reset();
				this.imgRefresh2.getAnimation().startNow();
			}
			
			this.parentView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
			this.parentView.requestLayout();
			
			this.parentView.setVisibility(View.VISIBLE);
			
			super.setUpdating(true);
			this.refreshFriendList();
			
		} else if (this.updateLayout1.getVisibility() == View.VISIBLE) {
			this.parentView.getLayoutParams().height = 0;
			this.parentView.requestLayout();
			
			this.parentView.setVisibility(View.GONE);
		}
		
	}
		
	private void refreshFriendList() {
		Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
		Session session = Session.openActiveSessionFromCache(this.context);
		
		if (session != null) {
			FacebookBGManager fbm = FacebookBGManager.getInstance(this.context);
			fbm.registerActionListener(this);
			Request.executeMyFriendsRequestAsync(session, this);
	
		}
	}

	@Override
	public void onRequestCompleted(Object sender, FacebookCommandObject fbco, int statusCode, Exception tr) {
		if (this.requestId == null) {
			MyLog.e(tag, "why here is null??");
			return;
		}
		
		if (this.requestId.equals(fbco.requestId())) {
			super.parentView.setVisibility(View.GONE);
			this.setUpdating(false);
			FacebookBGManager fbm = FacebookBGManager.getInstance(this.context);
			fbm.unregisterActionListener(this);
		}

	}

	@Override
	public void onCompleted(List<GraphUser> users, Response response) {
		Log.d(tag, "friendcount=" + users.size());
		
		int size = users.size();
		//size = 2;
		
		RefreshFriendsList a1 = new RefreshFriendsList(context, size);
		a1.setApplyCounter(false);
		this.requestId = a1.requestId();
		GeneralManager gm = GeneralManager.getInstance();
		gm.addNewQueue(a1, 2);
		
		int i = 0;
		for (GraphUser user : users) {
			
			//Log.d(tag, "name=" + user.getName() + ", " + user.getInnerJSONObject() + ", " + user.getBirthday());
			if (i < size) {
				
				/*
				TestFBAction2 a = new TestFBAction2(context, user.getId());
				a.setParent(a1);							
				gm.addNewQueue(a, 2);
				*/
				
				
				GetPhoto gp = new GetPhoto(context, user);
				gp.setParent(a1);
				gm.addNewQueue(gp, 2);
				
			}
			
			i += 1;
			
		}
	}

}
